package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.bean.YuShouShopBean;
import com.wnhz.work.yz.utils.GlideImageLoader;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;
import com.wnhz.work.yz.view.RoundImageView;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class YuShouActivity extends BaseActivity implements View.OnClickListener {
    private Banner banner;
    private ExpandableListView expandableListView;
    private YuShouShopBean bean = new YuShouShopBean();
    private List<YuShouShopBean.InfoEntity.CateListEntity> bean2 = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yu_shou);
        // MyApplication.addDestoryActivity(this, "yushoulist");
        initTitle();
        initView();
    }

    @Override
    protected void onResume() {
        upLoad();
        super.onResume();
    }

    private void upLoad() {
        Map<String, Object> map = new HashMap<>();
        showDialog();

        XUtil.Post(Url.BOOKING_BOOKINGLIST, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    if ("1".equals(res)) {
                        Gson gson = new Gson();
                        bean = gson.fromJson(result, YuShouShopBean.class);
                    } else {
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                //setData();
                h.sendEmptyMessage(1);
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
            }
        });
    }

    Handler h = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            setData();
            super.handleMessage(msg);
        }
    };
    private List<YuShouShopBean.InfoEntity.BannerEntity> bannerList;

    private void setData() {
        banner.setImageLoader(new GlideImageLoader());
        //设置图片集合
        List<String> url = new ArrayList<>();
        bannerList = bean.getInfo().getBanner();
        url.clear();
        for (int i = 0; i < bannerList.size(); i++) {
            url.add(bannerList.get(i).getPic());
        }
        banner.setImages(url);
        //banner设置方法全部调用完毕时最后调用
        banner.start();
        banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
//                if (MyApplication.getInstance().getUserBean() == null) {
//                    Toast.makeText(YuShouActivity.this, "请登录", Toast.LENGTH_SHORT).show();
//                    Intent intent2 = new Intent(YuShouActivity.this, LoginNewActivity.class);
//                    startActivity(intent2);
//                    return;
//                }
//                startActivity(new Intent(YuShouActivity.this, YuShouAllDetailssActivity.class).putExtra("goods_id", bannerList.get(position).getGoods_id()).putExtra("type", bannerList.get(position).getType()).putExtra("name", bannerList.get(position).getGoods_name()));
            }
        });
        bean2.clear();
        for (int i = 0; i < bean.getInfo().getCateList().size(); i++) {
            if (bean.getInfo().getCateList().get(i).getData().size() != 0) {
                bean2.add(bean.getInfo().getCateList().get(i));
            }
        }
        expandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
            @Override
            public void onGroupCollapse(int groupPosition) {
                for (int i = 0; i < bean2.size(); i++) {
                    if (i == groupPosition)
                        expandableListView.expandGroup(i);
                }
            }
        });
        expandableListView.setAdapter(new BaseExpandableListAdapter() {
            @Override
            public int getGroupCount() {
                return bean2.size();
            }

            @Override
            public int getChildrenCount(int groupPosition) {
                return bean2.get(groupPosition).getData().size();
            }

            @Override
            public Object getGroup(int groupPosition) {

                return bean2.get(groupPosition);
            }

            @Override
            public Object getChild(int groupPosition, int childPosition) {
                return bean2.get(groupPosition).getData().get(childPosition);
            }

            @Override
            public long getGroupId(int groupPosition) {
                return groupPosition;
            }

            @Override
            public long getChildId(int groupPosition, int childPosition) {
                return childPosition;
            }

            @Override
            public boolean hasStableIds() {
                return false;
            }

            @Override
            public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
                MyGroup group;
                if (convertView == null) {
                    group = new MyGroup();
                    convertView = View.inflate(YuShouActivity.this, R.layout.group_yushou, null);
                    group.title = (TextView) convertView.findViewById(R.id.title);
                    convertView.setTag(group);
                } else {
                    group = (MyGroup) convertView.getTag();
                }
                group.title.setText("" + bean2.get(groupPosition).getCate_name());
                return convertView;
            }

            class MyGroup {
                TextView title;
            }

            @Override
            public View getChildView(final int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
                Child child;
                if (convertView == null) {
                    child = new Child();
                    convertView = View.inflate(YuShouActivity.this, R.layout.child_yushou, null);
                    child.title = (TextView) convertView.findViewById(R.id.title);
                    child.title_class = (TextView) convertView.findViewById(R.id.title_class);
                    child.num1 = (TextView) convertView.findViewById(R.id.num1);
                    child.num2 = (TextView) convertView.findViewById(R.id.num2);
                    child.nums = (TextView) convertView.findViewById(R.id.nums);
                    child.shop_img = (RoundImageView) convertView.findViewById(R.id.shop_img);
                    child.progress = (ProgressBar) convertView.findViewById(R.id.progress);
                    child.person = (TextView) convertView.findViewById(R.id.person);
                    child.max_ll = (LinearLayout) convertView.findViewById(R.id.max_ll);
                    child.time_out = (TextView) convertView.findViewById(R.id.time_out);
                    child.out_of = (ImageView) convertView.findViewById(R.id.out_of);
                    convertView.setTag(child);
                } else {
                    child = (Child) convertView.getTag();
                }
                final YuShouShopBean.InfoEntity.CateListEntity.DataEntity data = bean2.get(groupPosition).getData().get(childPosition);
                if ("0".equals(data.getStatus())) {
                    child.time_out.setVisibility(View.VISIBLE);
                    child.out_of.setVisibility(View.GONE);
                    try {
                        String time = data.getBooking_time().substring(data.getBooking_time().indexOf(':'), data.getBooking_time().indexOf('.'));
                        child.time_out.setText("还剩" + time + "天");
                    } catch (Exception e) {
                        Log.e("--", e.toString());
                        child.time_out.setText(data.getBooking_time() + "");
                    }
                } else if ("1".equals(data.getStatus())) {
                    child.time_out.setVisibility(View.GONE);
                    child.out_of.setVisibility(View.VISIBLE);
                } else if ("2".equals(data.getStatus())) {
                    child.max_ll.setVisibility(View.GONE);
                }


                child.title.setText(data.getGoods_name());
//                if (!"整体浴室".equals(bean2.get(groupPosition).getCate_name())) {
//                    child.title_class.setBackground(getResources().getDrawable(R.drawable.button_ju));
//                } else {
//                    child.title_class.setBackground(getResources().getDrawable(R.drawable.button_main));
//                }

                if ("1".equals(bean2.get(groupPosition).getTypeid())) {
                    child.title_class.setBackground(getResources().getDrawable(R.drawable.button_ju));
                } else if ("0".equals(bean2.get(groupPosition).getTypeid())) {
                    child.title_class.setBackground(getResources().getDrawable(R.drawable.button_main));
                } else if ("2".equals(bean2.get(groupPosition).getTypeid())) {
                    child.title_class.setBackground(getResources().getDrawable(R.drawable.buttom_green));
                }
                child.title_class.setText(bean2.get(groupPosition).getCate_name());
                child.nums.setText("剩" + data.getGoods_else_nums() + "套");
                x.image().bind(child.shop_img, data.getGoods_img());

                child.progress.setMax(Integer.parseInt(data.getGoods_store()));
                child.progress.setProgress(Integer.parseInt(data.getGoods_else_nums()));
                child.person.setText(data.getBuy_user_nums() + "人");
                //
                child.num2.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, Float.parseFloat(data.getBuy_user_nums().toString())));
                child.num1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, Float.parseFloat(data.getGoods_else_nums().toString())));
                //
                child.max_ll.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (MyApplication.getInstance().getUserBean() == null) {
                            Toast.makeText(YuShouActivity.this, "请登录", Toast.LENGTH_SHORT).show();
                            Intent intent2 = new Intent(YuShouActivity.this, LoginNewActivity.class);
                            startActivity(intent2);
                            return;
                        }
                        startActivity(new Intent(YuShouActivity.this, YuShouAllDetailssActivity.class).putExtra("goods_id", data.getGoods_id()).putExtra("type", bean2.get(groupPosition).getType()).putExtra("name", data.getGoods_name()));
                    }
                });
                return convertView;
            }

            class Child {
                TextView title, title_class, num1, nums, num2, time_out;
                RoundImageView shop_img;
                ImageView out_of;
                ProgressBar progress;
                TextView person;
                LinearLayout max_ll;
            }

            @Override
            public boolean isChildSelectable(int groupPosition, int childPosition) {
                return false;
            }
        });
        for (int i = 0; i < bean2.size(); i++) {
            expandableListView.expandGroup(i);
        }
    }


    private void initView() {
        banner = (Banner) findViewById(R.id.banner);
        expandableListView = (ExpandableListView) findViewById(R.id.expanded_menu);
    }

    private void initTitle() {
        TextView title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("预售商品列表");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;
        }
    }
}
